/*
 * Copyright (c) 2010 CCX(China) Co.,Ltd. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * CCX(China) Co.,Ltd. ("Confidential Information").
 * It may not be copied or reproduced in any manner without the express 
 * written permission of CCX(China) Co.,Ltd.
 * 
 *	Created on 2010-12-13 下午04:27:43
 */
package cn.com.ccxe.core.util;

import java.util.Properties;

import org.apache.commons.lang.StringUtils;

/**
 * 解析和读取propertie格式的配置文件
 * @author hetao
 * 
 */
public final class Configurations {

	/**配置文件名称*/
	private static final String CONFIGURATIONS_FILE_NAME = "crawler.properties";
	private static Properties prop = new Properties();
	
	
	/**
	 * 获取配置文件资源。
	 */
	static {
		try {
			prop.load(Configurations.class.getClassLoader().getResourceAsStream(CONFIGURATIONS_FILE_NAME));
		} catch (Exception e) {
			prop = null;
			System.err.println("WARNING: Could not find crawler4j.properties file in class path. I will use the default values.");
		}
	}

	public static String getString(String key, String defaultValue) {
		if (prop == null) {
			return defaultValue;
		}
		String str = prop.getProperty(key);
		if(StringUtils.isEmpty(str)) {
			return defaultValue;
		}
		return prop.getProperty(key);
	}

	public static int getInt(String key, int defaultValue) {
		if (prop == null) {
			return defaultValue;
		}
		String str = prop.getProperty(key);
		if (StringUtils.isEmpty(str)) {
			return defaultValue;
		}
		return Integer.parseInt(str);
	}

	public static long getLong(String key, long defaultValue) {
		if (prop == null) {
			return defaultValue;
		}
		String str = prop.getProperty(key);
		if(StringUtils.isEmpty(str)) {
			return defaultValue;
		}
		return Long.parseLong(prop.getProperty(key));
	}

	public static boolean getBoolean(String key, boolean defaultValue) {
		if (prop == null) {
			return defaultValue;
		}
		String str = prop.getProperty(key);
		if(StringUtils.isEmpty(str)) {
			return defaultValue;
		}
		return prop.getProperty(key).toLowerCase().trim().equals("true");
	}

}
